<template>
  <el-dialog
    :title="`${drawerProps.title}环境`"
    v-model="drawerVisible"
    :draggable="true"
    :overflow="true"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    top="30px"
    width="80%"
    :destroy-on-close="true"
  >
    <el-form
      ref="ruleFormRef"
      style="height: 70vh; overflow-y: auto"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="drawerProps.row"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-row>
        <el-col :span="17">
          <group-title title="基本信息"></group-title>
          <el-row>
            <form-item title="服务名称" prop="name" :title-span="3" :item-span="9">
              <el-input v-model="drawerProps.row!.name" clearable></el-input>
            </form-item>
            <form-item title="操作系统" prop="system" :title-span="3" :item-span="9">
              <el-input v-model="drawerProps.row!.system" clearable></el-input>
            </form-item>
            <form-item title="用途" prop="use" :title-span="3" :item-span="21">
              <el-input type="textarea" v-model="drawerProps.row!.use" clearable></el-input>
            </form-item>
            <form-item title="IP地址" prop="ip" :title-span="3" :item-span="5">
              <el-input v-model="drawerProps.row!.ip" clearable></el-input>
            </form-item>
            <form-item title="CPU(核)" prop="cpu" :title-span="3" :item-span="5">
              <el-input type="number" v-model="drawerProps.row!.cpu" clearable></el-input>
            </form-item>
            <form-item title="内存(G)" prop="ram" :title-span="3" :item-span="5">
              <el-input type="number" v-model="drawerProps.row!.ram" clearable></el-input>
            </form-item>
            <form-item title="硬盘(G)" prop="hdd" :title-span="3" :item-span="5">
              <el-input type="number" v-model="drawerProps.row!.hdd" clearable></el-input>
            </form-item>
            <form-item title="安装的软件" prop="soft" :title-span="3" :item-span="5">
              <el-input v-model="drawerProps.row!.soft" clearable></el-input>
            </form-item>
            <form-item title="服务类型" prop="address" :title-span="3" :item-span="5">
              <el-input v-model="drawerProps.row!.address" clearable></el-input>
            </form-item>
          </el-row>
          <group-title title="使用情况"></group-title>
          <el-row>
            <form-item title="负责人" prop="ownerUser" :title-span="3" :item-span="5">
              <el-input v-model="drawerProps.row!.ownerUser" clearable></el-input>
            </form-item>
            <form-item title="使用状态" prop="resouceState" :title-span="3" :item-span="5">
              <el-input v-model="drawerProps.row!.resouceState" clearable></el-input>
            </form-item>
            <form-item title="使用期限" prop="useDate" :title-span="3" :item-span="5">
              <el-input v-model="drawerProps.row!.useDate" clearable></el-input>
            </form-item>
            <form-item title="重要级别" prop="level" :title-span="3" :item-span="5">
              <el-input v-model="drawerProps.row!.level" clearable></el-input>
            </form-item>
          </el-row>
          <group-title title="账号信息"></group-title>
          <el-row>
            <form-item title="账号" prop="account" :title-span="2" :item-span="4">
              <el-input v-model="drawerProps.row!.account" clearable></el-input>
            </form-item>
            <form-item title="密码" prop="pwd" :title-span="2" :item-span="4">
              <el-input type="password" show-password v-model="drawerProps.row!.pwd" clearable></el-input>
            </form-item>
          </el-row>
        </el-col>
        <el-col :offset="1" :span="6">
          <group-title title="操作记录"></group-title>
          <el-row>
            <business-action :business-id="drawerProps.row!._id"></business-action>
          </el-row>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="drawerVisible = false">取消</el-button>
        <el-button v-show="!drawerProps.isView" type="primary" @click="handleSubmit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="UserDrawer">
import { ref, reactive } from "vue";
import { ElMessage, FormInstance } from "element-plus";
import { Server } from "@/api/types/base";
import groupTitle from "@/components/application/form/group-title.vue";
import formItem from "@/components/application/form/form-item.vue";
import businessAction from "@/components/application/action/business-action.vue";
import { BusinessType, BusinessOptType } from "@/enums/sysEnum";
import { useUserAction } from "@/hooks/useUserAction";
import { useUserStore } from "@/stores/modules/user";

const { addUserAction } = useUserAction(BusinessType.SERVER);
const userStore = useUserStore();

const rules = reactive({
  name: [{ required: true, message: "请输入标题" }],
  ip: [{ required: true, message: "请输入IP地址" }],
  use: [{ required: true, message: "请输入用途" }]
});

interface DrawerProps {
  title: string;
  tbName: string;
  isView: boolean;
  row: Partial<Server>;
  api?: (tabName: string, params: any) => Promise<any>;
  getTableList?: () => void;
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  tbName: "",
  row: {}
});

// 接收父组件传过来的环境
const acceptParams = (params: DrawerProps) => {
  drawerProps.value = params;
  drawerVisible.value = true;
};

// 提交数据（新增/编辑）环境
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      if (drawerProps.value.title.indexOf("新增") != -1) {
        drawerProps.value.row.create_at = new Date();
        drawerProps.value.row.create_user = userStore.userInfo._id;
      }

      const data = await drawerProps.value.api!(drawerProps.value.tbName, drawerProps.value.row);
      ElMessage.success({ message: `${drawerProps.value.title}成功！` });
      if (drawerProps.value.title.indexOf("新增") != -1) {
        addUserAction(data._id, BusinessOptType.ADD, data.name);
      } else {
        addUserAction(drawerProps.value.row._id, BusinessOptType.UPDATE, drawerProps.value.row.name);
      }
      drawerProps.value.getTableList!();
      drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  acceptParams
});
</script>
